﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;

namespace Presentation
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        private UserBusiness _userBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private TextBox _oldPasswordTextBox;
        private TextBox _newPasswordTextBox;
        private TextBox _confirmPasswordTextBox;
        private Button _saveButton;
        private Button _resetButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _userBusiness = UserBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _oldPasswordTextBox = (TextBox)ChangePasswordUserControl.FindControl("OldPasswordTextBox");
                _newPasswordTextBox = (TextBox)ChangePasswordUserControl.FindControl("NewPasswordTextBox");
                _confirmPasswordTextBox = (TextBox)ChangePasswordUserControl.FindControl("ConfirmPasswordTextBox");
                _saveButton = (Button)ChangePasswordUserControl.FindControl("SaveButton");
                _resetButton = (Button)ChangePasswordUserControl.FindControl("ResetButton");
                _saveButton.Click += new EventHandler(SaveButton_Click);
                _resetButton.Click += new EventHandler(ResetButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("LogOn.aspx");
            }
            else
            {
                User user = (User)Session["user"];
                if (user.Role.RoleName == "Admin")
                {
                    Server.Transfer("NotAuthorized.aspx");
                }
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                User user = (User)Session["user"];
                User userToChangePassword = _userBusiness.GetById(user.UserId);
                string oldPassword = user.Password;
                string newPassword = _newPasswordTextBox.Text;
                if (oldPassword == _oldPasswordTextBox.Text)
                {
                    userToChangePassword.Password = newPassword;
                    _userBusiness.ChangePassword(userToChangePassword);
                    _successLabel.Text = "Change password success";
                }
                else
                {
                    _warningLabel.Text = "Old password is not correct";
                }
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {
            _oldPasswordTextBox.Text = "";
            _newPasswordTextBox.Text = "";
            _confirmPasswordTextBox.Text = "";
        }
    }
}